// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt
/* eslint-disable */

frappe.query_reports["Sample Admit PublicQuery"] = {
	"filters": [
		{
			"fieldname": "workflow_state",
			"fieldtype": "Select",
			"label": "工作流状态",
			"options": ['', '待提交','送测待审核','送测审核完成','测试结果已填写','测试结果已审核','测试结论初审完成','测试结论审批完成','Rejected'],
			"default": '',
			"width": 0
		},
		{
			"fieldname": "creation_from",
			"fieldtype": "Date",
			"label": "制单日期从",
			"default": frappe.datetime.add_days(frappe.datetime.get_today(), -360),
			"width": 0
		},
		{
			"fieldname": "creation_to",
			"fieldtype": "Date",
			"label": "制单日期到",
			"default": frappe.datetime.add_days(frappe.datetime.get_today(), 7),
			"width": 0
		},
		{
			"fieldname": "follow_status",
			"fieldtype": "Select",
			"label": "跟进状态",
			"options": ['', '编码提交前','编码提交完成','编码审核完成'],
			"default": '',
			"width": 0
		},
	],

	onload: function(report) {
		report.page.add_inner_button(__("New SRM Sample Admit"), function() {
			frappe.new_doc("SRM Sample Admit");
			// let new_doc = frappe.model.get_new_doc("SRM Sample Admit");
			// frappe.set_route("Form", new_doc.doctype, new_doc.name);
		}).addClass('btn-primary');
		// 默认划动右侧滚动条: -901这个距离
		// setInterval(function() {
		// 	$(".dt-scrollable").attr("height","35vw");
		// }, 5000);
		// console.log(report);
	},


	"formatter": function(value, row, column, data, default_formatter) {

		value = default_formatter(value, row, column, data);
		let format_fields = ["workflow_state"];

		if (format_fields && data && data["workflow_state"]) {
			var color_field = data["workflow_state"];
			let color = undefined;

			if (color_field == "测试结论审批完成") {
				color = 'green';
			}
			if (color_field == "Rejected") {
				color = 'red';
			}
			if (color_field == "送测待审核") {
				color = 'orange';
			}
			if (color_field == "送测审核完成") {
				color = 'blue';
			}
			if (color){
				value =`<div style='margin:0px;padding-left:5px;color:${color}!important;'>${value}</div>`            
			}	
		}

		return value;
	},

	after_datatable_render: function(datatable_obj) {
		setTimeout(() => {
			// 2023-09-07修订: 取消btn-sm隐藏, 因为导致了没有保存按钮
			// $('.btn-sm').hide();
			frappe.query_report.datatable.style.setStyle(`.dt-scrollable`, {height: '35vw'});
		}, 60);
	},

	get_datatable_options(options) {
        return Object.assign(options, {
            checkboxColumn: true,
			// events: {
			// 	onCheckRow: function(data) {
			// 		// var select_row = []
			// 		data[18].content = 'Y'
			// 		console.log(data[2].content)
			// 	},

			// 	onSwitchColumn: function(data) {
			// 		console.log('SwitchColumn')
			// 	},
			// }
        });
    },

};



	// onload: function(report) {
	// 	if (frappe.user.has_role('SRM Manager')) {
	// 		report.page.add_inner_button(__("测试"), function() {
	// 			// console.log(frappe.datetime.get_today())
	// 			var selected_rows = [];
	// 			$('.dt-scrollable').find(":input[type=checkbox]").each((idx, row) => {
	// 				if(row.checked){
	// 					selected_rows.push(frappe.query_report.data[idx]);
	// 					if (selected_rows.length > 1) {
	// 						frappe.throw('只允许操作一行!')
	// 					}
	// 					else {
	// 						frappe.confirm('品质交接确认?',
	// 						() => {
	// 							// action to perform if Yes is selected
	// 							console.log("*** selected row id : " + frappe.query_report.data[idx].name);
	// 						}, () => {
	// 							// action to perform if No is selected
	// 						})
	// 					}
	// 				}
	// 			});
	// 		}).addClass('btn-primary');
	// 	}
	// },